<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\controller;

/**
 * 文章接口控制器
 */
class Article extends ApiBase
{
    
    /**
     * 文章分类接口
     */
    public function categoryList()
    {
        
        return $this->apiReturn($this->logicArticle->getArticleCategoryList());
    }
    
    /**
     * 文章列表接口
     */
    public function articleList()
    {
        $data = $this->request->param();
        $filteredData = $this->filterApiParams($data, ['category_id', 'page', 'limit', 'keyword']);
        return $this->apiReturn($this->logicArticle->getArticleList($filteredData));
    }
    
    /**
     * 文章详情接口
     */
    public function getArticleInfo()
    {
        $data = $this->request->param();
        $filteredData = $this->filterApiParams($data, ['id', 'article_id']);
        return $this->apiReturn($this->logicArticle->getArticleInfo($filteredData));
    }
    
    /**
     * 过滤API参数，只保留指定的业务字段
     */
    private function filterApiParams($data, $allowedFields)
    {
        $filtered = [];
        foreach ($allowedFields as $field) {
            if (isset($data[$field])) {
                $filtered[$field] = $data[$field];
            }
        }
        return $filtered;
    }
}
